package com.proj.hsy;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.provider.MediaStore;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class InAppBrowserClient extends WebViewClient {

	WebViewInterface cordova;
	WebView webview;

	private String CloseChildeWebViewScheme = "close://";
	private String ShowChildeWebViewScheme = "showchildwebview://";
	private String ShowMainWebViewScheme = "showmainwebview://";

	private String OpenCaptureScheme = "opencapture://";
	private String OpenPhotoGalleryScheme = "openphotogallery://";

	public InAppBrowserClient(WebViewInterface cordova, WebView webview) {
		this.cordova = cordova;
		this.webview = webview;
	}

	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		try {
			if (url.startsWith(CloseChildeWebViewScheme)) {
				cordova.getActivity().finish();
				// 切换activity时候的动画
				// cordova.getActivity().overridePendingTransition(R.anim.in_from_left
				// , R.anim.out_to_right);
				return false;
			}
	
			else if (url.startsWith(ShowChildeWebViewScheme)) {
				Intent intent ;
				intent= new Intent(cordova.getActivity(),
						ChildWebView.class);
				//支持三级菜单
				if (cordova.getViewName().equals("ChildActivity")) {
					intent= new Intent(cordova.getActivity(),
							InnerChildWebView.class);
				}
				intent.putExtra("url", removeSecheme(url));
				cordova.getActivity().startActivity(intent);
				// 切换activity时候的动画
				// cordova.getActivity().overridePendingTransition(R.anim.in_from_right,
				// R.anim.out_to_left);
				return true;
			}
	
			else if (url.startsWith(ShowMainWebViewScheme)) {
				webview.loadUrl("file:///android_asset/www/" + removeSecheme(url));
				return true;
			}
	
			else if (url.startsWith(OpenCaptureScheme)) {
				String callbackIdString = GetUrlParam(url).getString("callbackId");
				cordova.setCallbackId(callbackIdString);
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				cordova.getActivity().startActivityForResult(intent, 0);
				return true;
			}
	
			else if (url.startsWith(OpenPhotoGalleryScheme)) {
				String callbackIdString = GetUrlParam(url).getString("callbackId");
				cordova.setCallbackId(callbackIdString);
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				cordova.getActivity().startActivityForResult(intent, 1);
				return true;
			}
	
			return true;
		} catch (JSONException e) {
			e.printStackTrace();
			return true;
		}
	}

	public JSONObject GetUrlParam(String url) throws JSONException {
		String paramString = url.substring(url.indexOf("?") + 1);
		JSONObject jsonObject = new JSONObject(paramString);
		return jsonObject;

	}

	public String removeSecheme(String url) {
		Pattern pattern = Pattern.compile(".*://");
		Matcher matcher = pattern.matcher(url);
		return matcher.replaceFirst("");
	}
}
